<template>
	<view>
		<view class="block__title">上报企业信息</view>
		<view class="cu-form-group">
			<view class="title">所属小区</view>
			<picker id="complaintType" bindchange="PickerChange" 
			:value="repairTypeIndex" 
			:range-key="'communityName'" 
			:range="repairTypes" 
			v-if = "repairTypes.length>0"
			 @change="repairTypeChange">
				<view class="picker">
					{{repairTypes.length==0 ? "请选择" : repairTypes[repairTypeIndex].communityName}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">企业名称</view>
			<input v-model="companyName" placeholder="请输入企业名称" class="text-right"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">统一信用代码</view>
			<input v-model="idCard" placeholder="请输入统一信用代码" class="text-right"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">法人</view>
			<input v-model="artificialPerson" placeholder="请输入法人" class="text-right"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">地址</view>
			<input v-model="companyAddress" placeholder="请输入地址" class="text-right"></input>
		</view>
		<view class="cu-form-group arrow">
			<view class="title">注册时间</view>
			<picker mode="date" :value="registerTime" start="1980-09-01" end="2025-09-01" @change="dateChange">
				<view class="picker">
					{{registerTime}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">负责人</view>
			<input v-model="personName" placeholder="请输入负责人" class="text-right"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">负责人身份证</view>
			<input v-model="personIdCard" placeholder="请输入负责人身份证" class="text-right"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">负责人电话</view>
			<input v-model="personTel" placeholder="请输入负责人电话" class="text-right"></input>
		</view>
		<view class="cu-form-group margin-top">
			<textarea v-model="ramark" placeholder="请输入备注"></textarea>
		</view>
		<view class="flex flex-direction">
			<button class="cu-btn bg-green margin-tb-sm lg" @click="submitRepair()">提交</button>
		</view>
	</view>
</template>

<script>
	// pages/enterCommunity/enterCommunity.js
	import {checkPhoneNumber,checkStrLength} from '../../lib/java110/utils/StringUtil.js'
	import {
		queryGovCommunity,saveGovCompany
	} from '../../api/company/companyApi.js'
	import {
		getAreaId
	} from '../../api/area/areaApi.js'
	export default {
		data() {
			return {
				registerTime: '请选择',
				govCommunityId: '',
				idCard: '',
				ramark: '',
				companyName: '',
				artificialPerson: '',
				companyAddress: '',
				personName: '',
				personIdCard: '',
				personTel: '',
				userId: '30518939884421922816',
				repairTypes: [],
				repairTypeIndex: 0,
			};
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			let that = this;
			this.vc.onLoad(options);
			that._loadCommunitys();
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {
		},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`;
				} else if (type === 'month') {
					return `${value}月`;
				} else if (type === 'day') {
					return `${value}日`;
				}
				return value;
			},
			submitRepair: function(e) {
				let obj = {
					"registerTime": this.registerTime,
					"govCommunityId": this.govCommunityId,
					"idCard": this.idCard,
					"companyName": this.companyName,
					"ramark": this.ramark,
					"artificialPerson": this.artificialPerson,
					"companyAddress": this.companyAddress,
					"personName": this.personName,
					"personIdCard": this.personIdCard,
					"personTel": this.personTel,
					"userId": this.userId,
					"companyType": '2002',
					"caId": getAreaId(),
				}
				console.log(obj);
				let msg = "";
				if (obj.govCommunityId == "") {
					msg = "请选择所属小区";
				} else if (obj.companyName == "") {
					msg = "请填写企业名称";
				} else if (obj.idCard == "") {
					msg = "请填写统一信用代码";
				}  else if (obj.registerTime == "请选择") {
					msg = "请选择注册时间";
				} else if (obj.artificialPerson == "") {
					msg = "请填写法人";
				}else if (obj.companyAddress == "") {
					msg = "请填写地址";
				}else if (obj.personName == "") {
					msg = "请填写负责人";
				}else if (obj.personIdCard == "") {
					msg = "请填写负责人身份证";
				}else if (obj.personTel == "") {
					msg = "请填写负责人电话";
				}else if (obj.ramark == "") {
					msg = "请填写报修内容";
				}
				if (msg != "") {
					wx.showToast({
						title: msg,
						icon: 'none',
						duration: 2000
					});
				} else {
					saveGovCompany(obj)
						.then(_data => {
							uni.showToast({
								icon: 'none',
								title: '企业信息上报成功'
							});
							wx.redirectTo({
								url: '/pages/companyManage/companyManage',
							});
						}, err => {
							uni.showToast({
								icon: 'none',
								title: err
							})
						})
				}
			},
			repairTypeChange: function(e) {
				this.repairTypeIndex = e.target.value //取其下标
				let selected = this.repairTypes[this.repairTypeIndex] //获取选中的数组
				if(selected == undefined){
					return;
				}
				this.govCommunityId = selected.govCommunityId //选中的id
			},
			dateChange: function(e) {
				this.registerTime = e.detail.value;
			},
			_loadCommunitys: function() {
				let _that = this;
				// 默认选择第一个
				_that.repairTypeIndex = 0;
				let dataObj = {
					page: 1,
					row: 50,
					caId: getAreaId()
				};
				queryGovCommunity(dataObj).then(_data =>{
					let _json = _data;
					if (_json.code == 0 && _json.data.length > 0) {
						_that.repairTypes = _json.data;
						let selected = _that.repairTypes[_that.repairTypeIndex] //获取选中的数组
						_that.govCommunityId = selected.govCommunityId //选中的id
					}else{
						uni.showToast({
							icon:"none",
							title:"未查询到小区信息"
						})
					}
				})
			}
		}
	};
</script>
<style>
	.block__title {
		margin: 0;
		font-weight: 400;
		font-size: 14px;
		color: rgba(69, 90, 100, .6);
		padding: 40rpx 30rpx 20rpx;
	}

</style>
